<?php	
	
	function __autoload($name){
		require_once  $name . '.php';
	}
	
	class UserLogin extends User{
		
		public function __construct($user, $pass){
			try{
				parent::__construct($user, $pass);
			}catch(Exception $e){
				print $e->getMessage();
			}
		}
		
		private function verifyUser(){
			
			$db = MysqlConfig::getConnection();
			if($db == false){ throw new Greska("<br/>Potrebna konekcija sa MYSQL BAZOM " . __CLASS__ . " -> " . __LINE__); } // stampa gresku ako nije uspostavio vezu sa MYSQL Bazom
			
			$sql = "select * from users;";
			$prepare = $db->prepare($sql);
			$prepare->execute();
			$res = $prepare->fetchAll(PDO::FETCH_ASSOC); // ucitavam sve iz baze
			
			foreach ($res as $row){ // provjeravam user i pass preko foreach kako bi izbjegavao sql injenction
				if($this->getUserName() == $row['name']){
					$hash_pass = hash("sha256", $this->getPassword(), false); // passwordi su sacuvani kao hash - sha256 u bazi podataka
					if($hash_pass == $row['pass']){
						return "1";
					}else{
						throw new Greska("<br/>Pogresna lozinka " . __CLASS__ . " -> " . __LINE__);
					}
				}else{
					throw new Greska("<br/>Korisnik ne postoji u bazi podata " . __CLASS__ . " -> " . __LINE__);
				}
			}
			
		}
		
		public function Authentication(){
			try{
				return $this->verifyUser();
			}catch(Exception $e){
				print $e->getMessage();
			}
		}
		
	}

?>